package control;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import swimv2.ejb.remote.GestioneAmiciRemote;
import swimv2.ejb.util.LoginToken;
import util.HtmlUtil;

/**
 * Servlet implementation class Amici
 */
public class Amici extends Servlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see Servlet#Servlet()
     */
    public Amici() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		showPaginaAmici(request,response);
		
	}

	private void showPaginaAmici(HttpServletRequest request,
			HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();

			try {
	        	Context jndiContext  = new javax.naming.InitialContext();
				Object ref1 = jndiContext.lookup("GestioneAmici/remote");
				GestioneAmiciRemote gestioneAmiciRemote =(GestioneAmiciRemote) PortableRemoteObject.narrow(ref1, GestioneAmiciRemote.class);
				
				request.setAttribute("amici", gestioneAmiciRemote.elencoAmici(username));
				request.setAttribute("richiesteEffettuate", gestioneAmiciRemote.elencoRichiesteInviate(username));	
				request.setAttribute("richiesteRicevute", gestioneAmiciRemote.elencoRichiesteRicevute(username));
				
				HtmlUtil.CompletaEdIncludi(request, response, "/WEB-INF/pages/elencoAmici.jsp");
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String azione=(String) request.getParameter("azione");
		if(azione.equals("confermaAmico"))
		{
			confermaAmico(request,response);
		} else if(azione.equals("rifiutaAmico"))
		{
			rifiutaAmico(request,response);
		}
	}

	private void confermaAmico(HttpServletRequest request,
			HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();
		String usernameAmico=(String) request.getParameter("amico");
			try {
	        	Context jndiContext  = new javax.naming.InitialContext();
				Object ref1 = jndiContext.lookup("GestioneAmici/remote");
				GestioneAmiciRemote gestioneAmiciRemote =(GestioneAmiciRemote) PortableRemoteObject.narrow(ref1, GestioneAmiciRemote.class);
				
				gestioneAmiciRemote.confermaAmico(username, usernameAmico);
				HtmlUtil.redirect(request, response, "/Amici");
			} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
	}
	
	private void rifiutaAmico(HttpServletRequest request,
			HttpServletResponse response) {
		LoginToken tok = (LoginToken) request.getSession().getAttribute("LoginToken");
		String username=tok.getUsername();
		String usernameAmico=(String) request.getParameter("amico");
			try {
	        	Context jndiContext  = new javax.naming.InitialContext();
				Object ref1 = jndiContext.lookup("GestioneAmici/remote");
				GestioneAmiciRemote gestioneAmiciRemote =(GestioneAmiciRemote) PortableRemoteObject.narrow(ref1, GestioneAmiciRemote.class);
				
				gestioneAmiciRemote.rifiutaAmico(username, usernameAmico);
				HtmlUtil.redirect(request, response, "/Amici");
				} catch (NamingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
} catch (ServletException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		
	}

}
